package com.example.framework.web.core.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author yuzhenxin
 * @date 2023-07-07 23:11
 * @description 自定义请求头拦截器
 */
@Slf4j
public class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
        log.debug("---------- 拦截器放行：preHandle，url: {} ----------", request.getRequestURL());
        return true;
    }

    @Override
    public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, ModelAndView modelAndView) {
        // 只有当这个拦截器的preHandle方法成功完成并返回true时，才会调用它！
        // 成功执行处理程序后执行
        log.debug("---------- 拦截器：postHandle ----------");
    }

    @Override
    public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) {
        // 只有当这个拦截器的preHandle方法成功完成并返回true时，才会调用它！
        // 抛异常也会执行
        log.debug("---------- 拦截器：afterCompletion ----------");
    }
}
